/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.salesOrderProcessing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import commonInfrastructure.system.entity.SystemParameterEntity;
import commonInfrastructure.system.session.SystemSessionBean;
import crm.clientManagement.entity.CompanyEntity;
import java.io.IOException;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import crm.salesOrderProcessing.entity.SalesQuotationEntity;
import crm.salesOrderProcessing.session.SalesQuotationSessionBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;


@ManagedBean
@ViewScoped
public class ConvertSalesQuotationManagedbean implements Serializable{

    private Long quotationId = null;
    @EJB
    SalesQuotationSessionBean salesQuotationSessionBean;
    @EJB
    SystemSessionBean systemSessionBean;
    @EJB
    EmployeeSessionBean employee;
    
    
    private SalesQuotationEntity temp;
    
    SystemParameterEntity systemParameter;
    CompanyEntity salesLead;

    /** Creates a new instance of ConvertSalesQuotationManagedbean */
    public ConvertSalesQuotationManagedbean() {
    }
    
//    public void initView(PhaseEvent event){
//        quotationList = salesQuotationSessionBean.getAllSalesQuotations();
//    }

    public void redirect(SalesQuotationEntity selectedQuotation) throws IOException {

        systemParameter = systemSessionBean.getSystemParameter();

        SalesQuotationEntity salesQuotation;
        
        if (selectedQuotation.isUsed()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "This Quotation is Used", ""));
            return;
        }

        if (selectedQuotation.isExpired()) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "This Quotation is Expired", ""));
            return;
        }

        if (!selectedQuotation.getCompany().getCompanyStatus().equals(systemParameter.getFullCustomerCompanyStatus())) {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("converting", selectedQuotation.getCompany());
            System.out.println("**********"+selectedQuotation.getCompany().getName());
            FacesContext.getCurrentInstance().getExternalContext().redirect("./../clientManagement/convertToCustomer.xhtml");
            return;
        }

//        if (!salesQuotation.getCompany().getSalesman().equals(systemSessionBean.getCurrentUser())){
//            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "This company is served by another salesman.", ""));
//            return;
//        }

//        salesQuotation.setUsed(true); Howard: I don't think we should set used to true at this stage as the salesorder may not be created 
//        salesQuotationSessionBean.saveChanges(salesQuotation);
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesQuotation", selectedQuotation);
        FacesContext.getCurrentInstance().getExternalContext().redirect("createNewSalesOrder.xhtml");
    }
    
    public void viewProcessing(ActionEvent event){ 
        setTemp((SalesQuotationEntity)event.getComponent().getAttributes().get("ViewQuotation"));
    }
    
    public List<SalesQuotationEntity> getSalesQuotations() {
        List<SalesQuotationEntity> quotationList = salesQuotationSessionBean.getAllSalesQuotation();
        for (int i=0;i<quotationList.size();i++){
            if (quotationList.get(i).isUsed()){
                quotationList.remove(i);
                i=i-1;
            }
        }
        return quotationList;
    }

    public Long getQuotationId() {
        return quotationId;
    }

    public void setQuotationId(Long quotationId) {
        this.quotationId = quotationId;
    }


    /**
     * @return the temp
     */
    public SalesQuotationEntity getTemp() {
        return temp;
    }

    /**
     * @param temp the temp to set
     */
    public void setTemp(SalesQuotationEntity temp) {
        this.temp = temp;
    }
}
